import ajax from '../../js/ajax.js';
import tools from '../../js/tools.js';

let xsxx = document.getElementById('xsxx');

let sz = [];
let cs1 = {
  cid: -1,
  orderBy: 2,
  phone: '',
  qq: '',
  sname: '',
  wechat: ''
};
let cs2 = {
  pageNumber: 1,
  pageSize: 4
};
//#region =========获取学生信息==========
function HQxs() {
  let cs = tools.concatJson(cs1, cs2);
  ajax.send('/manage/student/queryAll', cs, (Date) => {
    if (Date.success) {
      sz = Date.list;
      cs2 = Date.page;
      ZSxs();
      xsfy();
      return;
    }
    alert(Date.message);
  });
}
function ZSxs() {
  xsxx.innerHTML = '';
  for (let i = 0; i < sz.length; i++) {
    const x = sz[i];
    let tr = document.createElement('tr');
    let td;
    // 学生姓名
    td = document.createElement('td');
    td.append(x.sname);
    tr.append(td);
    // qq
    td = document.createElement('td');
    td.append(x.qq);
    tr.append(td);
    // 微信
    td = document.createElement('td');
    td.append(x.wechat);
    tr.append(td);
    // 手机号码
    td = document.createElement('td');
    td.append(x.phone);
    tr.append(td);
    // 家庭地址
    td = document.createElement('td');
    td.append(x.address);
    tr.append(td);
    // 信息最后修改时间
    td = document.createElement('td');
    td.append(tools.FormData(x.lastupdate));
    tr.append(td);

    //修改按钮
    td = document.createElement('td');
    let an1 = document.createElement('button');
    an1.append('修改');
    an1.addEventListener('click', () => {
      xgff(x);
    });
    td.append(an1);
    tr.append(td);

    //删除按钮
    td = document.createElement('td');
    let an2 = document.createElement('button');
    an2.append('删除');
    an2.addEventListener('click', () => {
      scff(x);
    });
    td.append(an2);
    tr.append(td);

    xsxx.append(tr);
  }
}
//#endregion

//#region =========获取班级信息和相关操作==========
let xzbj = document.getElementById('xzbj');
let xztc = document.getElementById('xztc');
let bjcx = document.getElementById('bjcx');
let Acx = document.getElementById('Acx');
let Aqx = document.getElementById('Aqx');
let bjxx = document.getElementById('bjxx');

//模式 查询：1  增加：2  修改：3
let ms = 1;

let sz1 = [];
let bjcs1 = {
  cname: ''
};
let bjcs2 = {
  pageNumber: 1,
  pageSize: 4
};
function HQbj() {
  let bjcs = tools.concatJson(bjcs1, bjcs2);
  ajax.send('/manage/class/queryAll', bjcs, (Date) => {
    if (Date.success) {
      sz1 = Date.list;
      bjcs2 = Date.page;
      ZSbj();
      fy();
      return;
    }
    alert(Date.message);
  });
}

function ZSbj() {
  bjxx.innerHTML = '';
  for (let i = 0; i < sz1.length; i++) {
    const y = sz1[i];
    let tr = document.createElement('tr');
    let td;

    //班级名称
    td = document.createElement('td');
    td.append(y.cname);
    tr.append(td);
    //操作
    td = document.createElement('td');
    let an = document.createElement('button');
    an.append('选择该班级');
    an.addEventListener('click', () => {
      //查询模式ms=1
      if (ms == 1) {
        cs1.cid = y.cid;
        xzbj.innerHTML = `${y.cname}`;
      } else if (ms == 2) {
        tjcs.cid = y.cid;
        xzbj2.innerHTML = `${y.cname}`;
      } else if (ms == 3) {
        xgcs.cid = y.cid;
        xzbj3.innerHTML = `${y.cname}`;
      }

      Aqx.click();
    });

    td.append(an);
    tr.append(td);

    bjxx.append(tr);
  }
}
xzbj.addEventListener('click', () => {
  ms = 1;
  xztc.showModal();
  HQbj();
});
Acx.addEventListener('click', () => {
  bjcs1.cname = bjcx.value;
  HQbj();
});
Aqx.addEventListener('click', () => {
  //触发表单
  document.querySelector('#xztc form').submit();
});
//#endregion

//#region =========查询==========
let pxff = document.getElementById('pxff');
let xmcx = document.getElementById('xmcx');
let dhcx = document.getElementById('dhcx');
let qqcx = document.getElementById('qqcx');
let wxcx = document.getElementById('wxcx');
let cx = document.getElementById('cx');
let cz = document.getElementById('cz');

cx.addEventListener('click', () => {
  cs1.orderBy = pxff.value;
  cs1.sname = xmcx.value;
  cs1.phone = dhcx.value;
  cs1.qq = qqcx.value;
  cs1.wechat = wxcx.value;

  cs2.pageNumber = 1;
  HQxs();
});
cz.addEventListener('click', () => {

  pxff.value = 2;
  xmcx.value = '';
  dhcx.value = '';
  qqcx.value = '';
  wxcx.value = '';
  cs2.pageNumber = 1;
  cx.click();
});
//#endregion

//#region =========增加==========
let zj = document.getElementById('zj');
let zjtc = document.getElementById('zjtc');
let xzbj2 = document.getElementById('xzbj2');
let tjxm = document.getElementById('tjxm');
let tjqq = document.getElementById('tjqq');
let tjwx = document.getElementById('tjwx');
let tjhm = document.getElementById('tjhm');
let tjdz = document.getElementById('tjdz');
let tj = document.getElementById('tj');
let qx = document.getElementById('qx');
let tjcs = {};
zj.addEventListener('click', () => {
  zjtc.showModal();
});
qx.addEventListener('click', () => {
  document.querySelector('#zjtc form').submit();
  HQxs();
});

xzbj2.addEventListener('click', () => {
  ms = 2;
  xztc.showModal();
  HQbj();
});
tj.addEventListener('click', () => {
  tjcs.sname = tjxm.value;
  tjcs.qq = tjqq.value;
  tjcs.wechat = tjwx.value;
  tjcs.phone = tjhm.value;
  tjcs.address = tjdz.value;
  ajax.send('/manage/student/add', tjcs, (Date) => {
    if (Date.success) {
      tjxm.value = '';
      tjqq.value = '';
      tjwx.value = '';
      tjhm.value = '';
      tjdz.value = '';
    }
    alert(Date.message);
  });
});

//#endregion

//#region =========修改===========
let xgtc = document.getElementById('xgtc');
let xzbj3 = document.getElementById('xzbj3');
let xgxm = document.getElementById('xgxm');
let xgqq = document.getElementById('xgqq');
let xgwx = document.getElementById('xgwx');
let xghm = document.getElementById('xghm');
let xgdz = document.getElementById('xgdz');
let xg = document.getElementById('xg');
let qx2 = document.getElementById('qx2');
let xgcs = {};
qx2.addEventListener('click', () => {
  document.querySelector('#xgtc form').submit();
});

xzbj3.addEventListener('click', () => {
  ms = 3;
  xztc.showModal();
  HQbj();
});
function xgff(xscs) {
  xgcs = xscs;
  xzbj3.innerHTML = `${xgcs.tbClass.cname}`;
  xgxm.value = xscs.sname;
  xgqq.value = xscs.qq;
  xgwx.value = xscs.wechat;
  xghm.value = xscs.phone;
  xgdz.value = xscs.address;
  xgtc.showModal();
}
xg.addEventListener('click', () => {
  xgcs.sname = xgxm.value;
  xgcs.qq = xgqq.value;
  xgcs.wechat = xgwx.value;
  xgcs.phone = xghm.value;
  xgcs.address = xgdz.value;
  ajax.send('/manage/student/update', xgcs, (Date) => {
    alert(Date.message);
  });
});
xgtc.addEventListener('close', () => {
  HQxs();
});
//#endregion

//#region =========删除============
function scff(sccs) {
  if (confirm(`是否删除：${sccs.sname}`)) {
    ajax.send('/manage/student/delete', { sid: sccs.sid }, (Date) => {
      alert(Date.message);
      HQxs();
    });
  }
}
//#endregion

//#region =========班级分页============
let fenye1 = document.querySelectorAll('nav>a');
let fenye2 = document.querySelector('nav>span');
function fy() {
  fenye2.innerHTML = `
  记录数/当前页/总页数
  ${bjcs2.total}/${bjcs2.pageNumber}/${bjcs2.pageCount}
  `;
}

fenye1[0].addEventListener('click', () => {
  if (bjcs2.pageNumber == 1) {
    return;
  }
  bjcs2.pageNumber = 1;
  HQbj();
});
fenye1[1].addEventListener('click', () => {
  bjcs2.pageNumber--;
  if (bjcs2.pageNumber < 1) {
    bjcs2.pageNumber = 1;
    return;
  }
  HQbj();
});
fenye1[2].addEventListener('click', () => {
  bjcs2.pageNumber++;
  if (bjcs2.pageNumber > bjcs2.pageCount) {
    bjcs2.pageNumber = bjcs2.pageCount;
    return;
  }
  HQbj();
});
fenye1[3].addEventListener('click', () => {
  if (bjcs2.pageNumber == bjcs2.pageCount) {
    return;
  }
  bjcs2.pageNumber = bjcs2.pageCount;
  HQbj();
});

//#endregion

//#region =========学生分页============
let a = document.getElementById('a');
let b = document.getElementById('b');
let c = document.getElementById('c');
let d = document.getElementById('d');
let e = document.getElementById('e');

function xsfy() {
  a.addEventListener('click', () => {
    if (cs2.pageNumber == 1) {
      return;
    }
    cs2.pageNumber = 1;
    HQxs();
  });
  b.addEventListener('click', () => {
    cs2.pageNumber--;
    if (cs2.pageNumber < 1) {
      cs2.pageNumber = 1;
      return;
    }
    HQxs();
  });
  c.innerHTML = `记录数/当前页/总页数${cs2.total}/${cs2.pageNumber}/${cs2.pageCount}`;
  d.addEventListener('click', () => {
    cs2.pageNumber++;
    if (cs2.pageNumber > cs2.pageCount) {
      cs2.pageNumber = cs2.pageCount;
      return;
    }
    HQxs();
  });
  e.addEventListener('click', () => {
    if (bjcs2.pageNumber == bjcs2.pageCount) {
      return;
    }
    bjcs2.pageNumber = bjcs2.pageCount;
    HQxs();
  });
}
// #endregion

// #region
//#endregion
HQxs();
